package com.base.kotlin.mvvm

import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.Observer
import android.os.Looper
import java.lang.Exception

/**
 * 公共BaseLiveData出口
 */
open class BaseLiveData<T> : MutableLiveData<T>() {

    override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
        try {
            super.observe(owner, observer)
        } catch (e: Exception) {

        }
    }

    override fun observeForever(observer: Observer<T>) {
        try {
            super.observeForever(observer)
        } catch (e: Exception) {

        }
    }

    fun onValue(data: T) {
        if (value != null) {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                value = data
            } else {
                postValue(data)
            }
        }
    }
}